package com.shop.supply.controller;

import com.alibaba.fastjson.JSON;

import com.github.pagehelper.PageInfo;
import com.shop.supply.entity.SupplierInfo;
import com.shop.supply.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @description：控制层
 * @author：xyj
 * @date：2021/7/20 14:42
 * @versions：v1.0
 */
@Controller
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @RequestMapping("/supplierAdd")
    public String add(String supCode, String supName, String supDesc, String supContact, String supPhone, String supAddress, String supFax, Integer createdUserId, Date createdTime, Date updatedTime, Integer updatedUserId) {

        supplierService.add(supCode, supName, supDesc, supContact, supPhone, supAddress, supFax,createdUserId,createdTime,updatedTime,updatedUserId);

        return "providerlist";
    }

    @RequestMapping("add")
    public String add(){
        return "provideradd";
    }

    /**
     * 根据供货商id查询详细信息
     * @param model
     * @param id 供货商id
     * @param pro 操作码
     * @return
     */
    @RequestMapping("/getSupplierid")
    public String getSupplier(Model model, Integer id,@RequestParam(value = "pro",defaultValue = "xiangq") String pro) {
        SupplierInfo supplier = supplierService.getSupplierid(id);
        model.addAttribute("supplier",supplier);
        System.out.println(pro.equals("xiangq"));
       if(pro.equals("xiangq")){

           return "providerview";
       }

           return "providermodify";

    }

    /**
     * 查询供货商详细信息
     * @param model
     * @param curr 当前页码
     * @param pageSize 每次查询条数
     * @param supCode 供货商编码
     * @param supName 供货商名称
     * @return
     */
    @RequestMapping("/getSupplier")
    public String getSupplier(Model model, @RequestParam(value ="curr",defaultValue = "1" ) Integer curr, @RequestParam(defaultValue = "5") Integer pageSize, String supCode, String supName) {
        List<SupplierInfo> supplier = supplierService.getSupplier(curr, pageSize, supCode, supName);
        PageInfo<SupplierInfo> supplierInfo=new PageInfo<>(supplier);
        model.addAttribute("supplierInfo",supplierInfo);
        model.addAttribute("supCode",supCode);
        model.addAttribute("supName",supName);
        return "providerlist";
    }

    /**
     * 根据id删除供货商信息
     * @param id 供货商id
     * @return
     */
    @RequestMapping("supplierDel")
    @ResponseBody
    public String supplierDel(Integer id) {
        boolean integer = supplierService.supplierDel(id);

        return JSON.toJSONString(integer);
    }

    /**
     *
     * @param id
     * @param supCode
     * @param supName
     * @param supDesc
     * @param supContact
     * @param supPhone
     * @param supAddress
     * @param supFax
     * @return
     */
    @RequestMapping("/supplierUpdate")
    public String supplierUpdate(Integer id, String supCode, String supName, String supDesc, String supContact, String supPhone, String supAddress, String supFax) {
        supplierService.supplierUpdate(id, supCode, supName, supDesc, supContact, supPhone, supAddress, supFax);

        return "providerlist";
    }
    @RequestMapping("head")
    public String xx(){
        return "head";
    }
}
